//
//  ActivityCenterViewController.m
//  兼职页面
//
//  Created by kuiyuan liang on 2021/6/6.
//

#import "ActivityCenterViewController.h"
#import "CommonUtil.h"
#import "ActivityCenterView.h"
#import "ActivityTableViewCell.h"
#import "ActivityModel.h"
#import "CustonNavView.h"
#import "DayTableViewCell.h"
#import "MedalTableViewCell.h"

@interface ActivityCenterViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) CustonNavView * navView;
@property (strong, nonatomic) ActivityCenterView * headerView;
@property (strong, nonatomic) UITableView * tableView;
@property (strong, nonatomic) NSMutableArray * models;
@property (strong, nonatomic) UIColor * originColor;

@property (assign, nonatomic) NSInteger status;
@end

@implementation ActivityCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    self.navigationItem.title = @"活动中心";
    self.view.backgroundColor = RGB(239, 239, 239);
    [self.view addSubview:self.navView];
    [self.view addSubview:self.tableView];
    self.models = [NSMutableArray array];
    for (NSInteger i = 0; i < 10; i++) {
        ActivityModel *m = [ActivityModel new];
        m.count = 3;
        m.totalCount = 10;
        m.content = @"用户购买";
        if (i == 3 || i == 5) {
            m.totalCount = 3;
        }
        
        if (i == 9) {
            m.atLast = YES;
            m.content = @"用户购买用户购买用户购买用户购买用户购买用户购买用户购买用户购买用户购买用户购买用户购买用户购买用户购买用户购买用户购买用户购买用户购买用户购买用户购买用户购买用户购买用户购买用户购买用户购买用户购买用户购买";
        }
        
        [self.models addObject:m];
    }
    [self.tableView reloadData];
    self.originColor = self.navigationController.navigationBar.barTintColor;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = false;
}

- (void)backBtnClick:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.models.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.status == 0) {
        ActivityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        cell.model = self.models[indexPath.row];
        return cell;
    }else if (self.status == 1){
        DayTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
        return  cell;
    }else{
        MedalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell3"];
        cell.model = self.models[indexPath.row];
        return  cell;
    }
    
}

#pragma mark - UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    


}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.status == 2) {
        ActivityModel *m = self.models[indexPath.row];
        return m.cellHeight;
    }
    return AD(100);
}



-(CustonNavView *)navView{
    if (!_navView) {
        _navView = [[CustonNavView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kNavBarAndStatusBarHeight)];
        [_navView.backBtn addTarget:self action:@selector(backBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _navView;
}

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-kNavBarAndStatusBarHeight)];
        [_tableView registerClass:[ActivityTableViewCell class] forCellReuseIdentifier:@"cell1"];
        [_tableView registerClass:[DayTableViewCell class] forCellReuseIdentifier:@"cell2"];
        [_tableView registerClass:[MedalTableViewCell class] forCellReuseIdentifier:@"cell3"];
        
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = self.headerView;
        
        _tableView.contentInset = UIEdgeInsetsMake(0, 0, kTabBarHeight, 0);
        _tableView.backgroundColor = [UIColor clearColor];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
//iOS11系统及以上，
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
            // Fallback on earlier versions
        }
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

-(ActivityCenterView *)headerView{
    if (!_headerView) {
        _headerView = [[ActivityCenterView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0)];
        _headerView.model = [ActivityModel new];
        __weak typeof(self) weakSelf = self;
        _headerView.actionBlock = ^(NSInteger index) {
            weakSelf.status = index;
            [weakSelf.tableView reloadData];
        };
    }
    return _headerView;
}
@end
